<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Service;

class TagScoreService{
	
    /**
     * 保存标签和谱单的关系
     * @return unknown
     */
    public function save($tag_ids, $score_id){
        //step-0: 先删除历史数据
        $Model = D('TagScore');
        $Model->where(array('score_id'=>$score_id))->delete();
        
        //step-1: 组织标签和谱单的关系
        $tag_score = array();
        foreach ($tag_ids as $key=>$tag_id) {
            $tag_score[] = array(
                'score_id' => $score_id,
                'tag_id' => $tag_id,
                'creator'  => UID
            );
        }
        
        //step-2: 批量保存标签和谱单的关系
        return $Model->addAll($tag_score);
        
	}
	
	
	public function getTagsByScoreIds($scoreIdList){
	    //查询创建人信息
	    $map = array('score_id' => array('in', $scoreIdList));
	    
	    $Model = M('TagScore ts');
	    $tagList = $Model->field('ts.score_id, group_concat(t.name) name')
	           ->join('tag t on ts.tag_id=t.id')
	           ->group('ts.score_id')->where($map)->select();
	    
	    $tagMap = array();
	    foreach ($tagList as $key=>$tag) {
	        $tagMap[$tag['score_id']] = $tag['name'];
	    }
	    
	    return $tagMap;
	}
}